package com.ellecity06.cwhiskymall.ui.consumer_gift

import com.ellecity06.common.base.BaseMviViewModel
import com.ellecity06.common.ktx.postEvent
import com.ellecity06.cwhiskymall.constant.BusinessConstans
import com.ellecity06.cwhiskymall.http.request

/**
 * @author ellecity06
 * @time 2024/10/29 16:39
 * @des
 */
class ConsumerGiftViewModel : BaseMviViewModel<ConsumerGiftState, ConsumerGiftIntent>() {
    private var receiveStatus: Int = -1
    private var totalAmount: Double = 0.00
    override suspend fun collectIntent(it: ConsumerGiftIntent) {
        when (it) {
            is ConsumerGiftIntent.FetchData -> {
                fetchData()
            }
            is ConsumerGiftIntent.GoReceive -> {
                goReceive(it.id)
            }
            is ConsumerGiftIntent.SupReceive -> {
                subReceive(it.id, it.contributionLevel)
            }
            is ConsumerGiftIntent.AddBtnAction -> {
                // 按钮点击的意图
                if (receiveStatus == 1) {
                    // 去消费
                    mState.postEvent(ConsumerGiftState.ToShopping)
                } else {
                    // 去领取 ,判断一下当前档位 >1792 就可以领取1200贡献金 ，>3600 领取3600 >=6000
                    val amount = if (totalAmount >= BusinessConstans.levelList[2]) {
                        BusinessConstans.level3
                    } else {
                        if (totalAmount >= BusinessConstans.levelList[1]) {
                            BusinessConstans.level2
                        } else {
                            BusinessConstans.level1
                        }
                    }
                    mState.postEvent(ConsumerGiftState.ShowAlear(amount))
                }
            }
            else -> {}
        }
    }

    /**
     * 去领取 ，id为空表示是首次领取
     */
    private fun goReceive(id: Int?) {
        request(
            api = { firstReceiveContribution(id) },
            showLoading = true,
            success = {
                if (it != null) {
                    if (it.nextRound == true) {
                        // 去选择升级下一轮,
                        mState.postEvent(
                            ConsumerGiftState.ShowSelect(
                                id,
                                it.contributionLevelMoney!!,
                                it.contribution!!
                            )
                        )
                    } else {
                        // 弹窗提示领取结果
                        mState.postEvent(ConsumerGiftState.ShowResult(it.contribution!!))
                    }
                }
            })
    }

    /**
     * 叠加领取
     */
    private fun subReceive(id: Int, contributionLevel: String) {
        request(api = {
            superposeReceiveContribution(
                id,
                contributionLevel
            )
        }, showLoading = true, success = {
            if (it != null) {
                // 弹窗提示领取结果
                mState.postEvent(ConsumerGiftState.SubReceiveSuccess)

            }
        })
    }

    private fun fetchData() {
        request(api = { contributionRecordList() }, success = {
            if (it != null) {
                receiveStatus = it.receiveStatus ?: -1
                totalAmount = it.totalAmount ?: 0.00
                mState.postEvent(ConsumerGiftState.RecordSuccess(it))
            }
        })
    }
}